<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Service\Agent;

use App\Dao\AgentBankDao;
use App\Dao\AgentDao;
use App\Dao\BusinessDao;
use App\Dao\UserDao;
use App\Kernel\Util\AgentJwtInstance;
use App\Service\Service;

use Hyperf\Di\Annotation\Inject;

/**
 * 首页服务
 *
 *
 * @author 张昊(2396675557@qq.com)
 * @package App\Service\Admin
 */
class MainService extends Service
{

    /**
     * @Inject
     * @var MoneyService
     */
    public $money_service;

    /**
     * @Inject
     * @var BusinessDao
     */
    public $business_dao;

    /**
     * @Inject
     * @var UserDao
     */
    public $user_dao;

    /**
     * @Inject
     * @var AgentDao
     */
    public $agent_dao;

    /**
     * 平台首页服务
     */
    public function main()
    {
        $agent = AgentJwtInstance::instance()->build()->getAgent();

        $time_start = strtotime(date('Y-m-d'.'00:00:00',time()-3600*24));
        $time_end = strtotime(date('Y-m-d'.'00:00:00',time()));

        $business_where = [];
        $channel_where = [];
        switch ( $agent['identity'] ) {
            case "1": //区代
                $data['store_money_config'] = sConfig("area_agent_store_money") * 100;
                $data['channel_money_config'] = sConfig("area_agent_channel_money") * 100;

                $business_where = [
                    'district_id' => $agent['location_id'],
                    'status' => 1,
                ];

                $channel_where = [
                    'district_id' => $agent['location_id'],
                    'group_id' => 2,
                ];

                break;
            case "2": //市代
                $data['store_money_config'] = sConfig("city_agent_store_money") * 100;
                $data['channel_money_config'] = sConfig("city_agent_channel_money") * 100;
                $business_where = [
                    'city_id' => $agent['location_id'],
                    'status' => 1,
                ];
                $channel_where = [
                    'city_id' => $agent['location_id'],
                    'group_id' => 2,
                ];
                break;
            case "3": //分公司
                $data['office_area_money_config'] = $agent['area_agent_money']?$agent['area_agent_money'] * 100:sConfig("office_area_money") * 100;
                $data['office_city_money_config'] = $agent['city_agent_money']?$agent['city_agent_money'] * 100:sConfig("office_city_money") * 100;
                $data['office_channel_money_config'] = $agent['channel_agent_money']?$agent['channel_agent_money'] * 100:sConfig("office_channel_money") * 100;
                break;
            case "4": //合伙人
                $data['partner_area_money_config'] = $agent['area_agent_money']?$agent['area_agent_money'] * 100:sConfig("partner_area_money") * 100;
                $data['partner_city_money_config'] = $agent['city_agent_money']?$agent['city_agent_money'] * 100:sConfig("partner_city_money") * 100;
                $data['partner_channel_money_config'] = $agent['channel_agent_money']?$agent['channel_agent_money'] * 100:sConfig("partner_channel_money") * 100;
                break;
        }

        $data['yesterday_store_count'] = $this->money_service->agent_money_dao->monthCount([
            'agent_id' => agentId(),
            'type' => 1,
            'price_source' => 1,
        ],$time_start,$time_end);

        $data['yesterday_channel_count'] = $this->money_service->agent_money_dao->monthCount([
            'agent_id' => agentId(),
            'type' => 1,
            'price_source' => 2,
        ],$time_start,$time_end);

        $business_count = $this->business_dao->count($business_where);
        $channel_count = $this->user_dao->count($channel_where);
        $data['business_count'] = $business_count ?? 0;
        $data['channel_count'] = $channel_count ?? 0;
        $data['total_revenue'] = $this->money_service->getMoneyBalanceByAgent(agentId());
        return $data;
    }

    /**
     * 分公司首页
     */
    public function company()
    {
        $agent = AgentJwtInstance::instance()->build()->getAgent();

        $time_start = strtotime(date('Y-m-d'.'00:00:00',time()-3600*24));
        $time_end = strtotime(date('Y-m-d'.'00:00:00',time()));

        $area = $this->agent_dao->mapMany([
            'identity' => 1,
            'inviter_id' => $agent['id'],
        ],false);

        $ids = [];
        foreach ($area as $key => $value) {
            $ids[] = $value['location_id'];
        }

        $area_channel_count = $this->user_dao->count([
            'district_id' => ['in', $ids],
            'group_id' => 2
        ]);

        $area_where = [];
        $city_where = [];
        $partner_where = [];
        switch ( $agent['identity'] ) {
            case "3": //分公司
                $area_where = [
                    'identity' => 1,
                    'inviter_id' => $agent['id'],
                ];
                $city_where = [
                    'identity' => 2,
                    'inviter_id' => $agent['id'],
                ];
                $partner_where = [
                    'identity' => 4,
                    'inviter_id' => $agent['id'],
                ];
                $data['office_area_money_config'] = $agent['area_agent_money']?$agent['area_agent_money'] * 100:sConfig("office_area_money") * 100;
                $data['office_city_money_config'] = $agent['city_agent_money']?$agent['city_agent_money'] * 100:sConfig("office_city_money") * 100;
                $data['office_channel_money_config'] = $agent['channel_agent_money']?$agent['channel_agent_money'] * 100:sConfig("office_channel_money") * 100;
                break;
            case "4": //合伙人
                $data['partner_area_money_config'] = $agent['area_agent_money']?$agent['area_agent_money'] * 100:sConfig("partner_area_money") * 100;
                $data['partner_city_money_config'] = $agent['city_agent_money']?$agent['city_agent_money'] * 100:sConfig("partner_city_money") * 100;
                $data['partner_channel_money_config'] = $agent['channel_agent_money']?$agent['channel_agent_money'] * 100:sConfig("partner_channel_money") * 100;
                break;
        }

        $data['yesterday_amount_total'] = $this->money_service->agent_money_dao->monthMoney([
            'agent_id' => agentId(),
            'type' => 1,
        ],$time_start,$time_end);

        $yesterday_partner_count = $this->agent_dao->monthCount([
            'identity' => 4,
            'inviter_id' => agentId(),
        ],$time_start,$time_end);

        $yesterday_city_count = $this->agent_dao->monthCount([
            'identity' => 2,
            'inviter_id' => agentId(),
        ],$time_start,$time_end);

        $yesterday_area_count = $this->agent_dao->monthCount([
            'identity' => 1,
            'inviter_id' => agentId(),
        ],$time_start,$time_end);

        $area_count = $this->agent_dao->count($area_where);
        $city_count = $this->agent_dao->count($city_where);
        $partner_count = $this->agent_dao->count($partner_where);

        $data['total_amount'] = $this->money_service->getMoneyBalanceByAgent(agentId());
        $data['area_count'] = $area_count ?? 0;
        $data['city_count'] = $city_count ?? 0;
        $data['channel_count'] = $area_channel_count ?? 0;
        $data['partner_count'] = $partner_count ?? 0;

        $data['yesterday_area_count'] = $yesterday_area_count ?? 0;
        $data['yesterday_city_count'] = $yesterday_city_count ?? 0;
        $data['yesterday_partner_count'] = $yesterday_partner_count;

        return $data;
    }

}